import java.io.File;
import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Picture extends HtmlAble{
	
	String Photo;
	MyDate date;
	String Location;
	String Place;
	String Activity;
	ArrayList<String> People;
	ArrayList<String> Food;
	TravelPlans TravPlans;
	String TimeOfDay;
	ArrayList<String> Expenses;
	ArrayList<String> Album;
	ArrayList<String> Text;
	public static File parentFile;
	public static SimpleDateFormat dateFormat = new SimpleDateFormat();
	private static Pattern pat = Pattern.compile("(?:(\\d{1,2}|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)?(?:\\D+(\\d{1,2}))??\\D+)??(\\d{2}(?:\\d{2})?)");
			
	public Picture()
	{
            People = new ArrayList<String>();
            Food = new ArrayList<String>();
            Expenses = new ArrayList<String>();
            Album = new ArrayList<String>();
            Text = new ArrayList<String>();
            TravPlans = new TravelPlans();
	}



	public String getPhoto() {
		return Photo;
	}



	public void setPhoto(String photo) {
		Photo = photo;
	}



	public MyDate getDate() {
		return date;
	}

	public void setDate(String _date) {
		date = new MyDate(_date);
	}

	//Allows me to parse random gregorian date representations and sort them
	public static class MyDate implements Comparable
	{
		Calendar dateObj;
		String date;
		private static Hashtable<String, Integer> monthToNums = new Hashtable<String,Integer>();
		static
		{
			monthToNums.put("jan",0);
			monthToNums.put("feb",1);
			monthToNums.put("mar",2);
			monthToNums.put("apr",3);
			monthToNums.put("may",4);
			monthToNums.put("jun",5);
			monthToNums.put("jul",6);
			monthToNums.put("aug",7);
			monthToNums.put("sep",8);
			monthToNums.put("oct",9);
			monthToNums.put("nov",10);
			monthToNums.put("dec",11);
		}
		
		public MyDate(String _date)
		{
			date = _date;
			Matcher mat = pat.matcher(date.toLowerCase());
			if(mat.matches())
			{
				dateObj = Calendar.getInstance();
				dateObj.setTimeInMillis(0);
				if(mat.group(3)!=null)
				{
					int yearInt = Integer.parseInt(mat.group(3));
					if(yearInt < 100)
					{
						int currentYear = Calendar.getInstance().get(Calendar.YEAR);
						if( yearInt > currentYear%100 )
						{
							dateObj.set(Calendar.YEAR, yearInt + ((currentYear/100-1) * 100));
						}
						else
						{
							int year = ((currentYear/100) * 100);
							dateObj.set(Calendar.YEAR, year+yearInt);
						}
					}
					else
					{
						dateObj.set(Calendar.YEAR,yearInt);
					}
				}
				if(mat.group(1)!=null)
				{
					String month = mat.group(1);
					if(!Character.isDigit(month.charAt(0)))
					{
						dateObj.set(Calendar.MONTH, monthToNums.get(month));
					}
					else
					{
						int monthInt = Integer.parseInt(month);
						if(monthInt<=0 || monthInt >12)
						{
							dateObj = null;
							return;
						}
						dateObj.set(Calendar.MONTH, monthInt - 1);
					}
				}
				if(mat.group(2)!=null)
				{
					int dayInt = Integer.parseInt(mat.group(2));
					if(dayInt <= 0 || dayInt > dateObj.getActualMaximum(Calendar.DAY_OF_MONTH))
					{
						dateObj = null;
						return;
					}
					dateObj.set(Calendar.DAY_OF_MONTH,dayInt);
				}
			}
		}
		
		public int compareTo(Object arg0) {
			MyDate myDate = (MyDate)arg0;
			return this.dateObj.compareTo(myDate.dateObj);
		}
		
		public String toString()
		{
			return date;
		}
	}
	
	public String getLocation() {
		return Location;
	}



	public void setLocation(String location) {
		Location = location;
	}



	public String getPlace() {
		return Place;
	}



	public void setPlace(String place) {
		Place = place;
	}



	public String getActivity() {
		return Activity;
	}



	public void setActivity(String activity) {
		Activity = activity;
	}



	public ArrayList<String> getPeople() {
		return People;
	}



	public void setPeople(ArrayList<String> people) {
		People = people;
	}



	public ArrayList<String> getFood() {
		return Food;
	}



	public void setFood(ArrayList<String> food) {
		Food = food;
	}



	public TravelPlans getTravelPlans() {
		return TravPlans;
	}



	public void setTravelPlans(TravelPlans travelPlans) {
		TravPlans = travelPlans;
	}



	public String getTimeOfDay() {
		return TimeOfDay;
	}



	public void setTimeOfDay(String timeOfDay) {
		TimeOfDay = timeOfDay;
	}



	public ArrayList<String> getExpenses() {
		return Expenses;
	}



	public void setExpenses(ArrayList<String> expenses) {
		Expenses = expenses;
	}



	public ArrayList<String> getAlbum() {
		return Album;
	}



	public void setAlbum(ArrayList<String> album) {
		Album = album;
	}



	public ArrayList<String> getText() {
		return Text;
	}



	public void setText(ArrayList<String> text) {
		Text = text;
	}

	public String toString()
	{
		return getPhoto();
	}
		
	public String getTags()
	{
		String rep = " ";
		if(getLocation() != null)
			rep += getLocation()+" ";
		if(getPlace() != null)
			rep += getPlace()+" ";
		for(String food: Food)
			rep += food + " ";
		if(getActivity() != null)
			rep += getActivity() + " ";
		return rep;
	}
		
	public URI getURI()
	{
		File pictureFile = new File(parentFile.getParentFile(), getPhoto());
		if (pictureFile.isFile())
			return pictureFile.toURI();
		else
			return null;
	}
	
	public String getHTML()
	{
		String rep = "<b><strong>Picture:</strong></b><br/>";
		rep += 	((getDate() != null)?"<b>Date: " + getDate() + "</b><br/>":"");
		rep += 	((getTimeOfDay() != null)?"<b>Time: " + getTimeOfDay() + "</b><br/>":"");
			
		rep +=  ((getLocation() != null)?"<b>Location: " + getLocation() + "</b><br/>":"") +
				((getPlace() != null)?"<b>Place: " + getPlace() + "</b><br/>":"");
		
		rep += ((getActivity() != null)?"<b>Activity: " + getActivity() + "</b><br/>":"");
		
		if(People.size() > 0)
		{
				rep+= "<b>People: </b><br/>" + People.get(0);
				for(int x=1; x < People.size(); x++) 
				{
					rep+= ", " + People.get(x);
				}
				rep+= "<br/>";
		}
		
		
		if(Food.size() > 0)
		{
				rep+= "<b>Food: </b><br/>" + Food.get(0);
				for(int x=1; x < Food.size(); x++) 
				{
					rep+= ", " + Food.get(x);
				}
				rep+= "<br/>";
		}
		
		if(Expenses.size() > 0)
		{
				rep+= "<b>Expenses:</b><br/>" + Expenses.get(0);
				for(int x=1; x < Expenses.size(); x++) 
				{
					rep+= ", " + Expenses.get(x);
				}
				rep+= "<br/>";
		}
		
		rep += this.getTravelPlans().getHTML();
	
		if(Text.size() > 0)
		{
				rep+= "<b>Comments:</b><br/>"+Text.get(0)+"<br/>";
				for(int x=1; x < Text.size(); x++) 
				{
					rep+= Text.get(x) +"<br/>";
				}
				rep+= "<br/>";
		}
		return rep;	
	}
	

}


